// 定义配置公用的一些工具类，例如设备屏幕的宽高、屏幕适配、判空处理、时间处理等
// 防抖，忘了
import debounce from 'lodash/debounce';
// 节流，用于一定时间内触发的事件只会执行一次，就好比点击跳转但是两秒内点击五次但是之后跳转一次
import throttle from 'lodash/throttle';
import { BackHandler, Dimensions, ToastAndroid } from 'react-native';
import { NavigationActions, StackActions } from "react-navigation";

export const resetAction = StackActions.reset({
    index: 0,
    actions: [
        NavigationActions.navigate({ routeName: 'MainTab' })
    ]
})
const deviceWidth = Dimensions.get('window').width;
const uiWidth = 414;
let time = 0;
// 计算ui尺寸
const pxPage = (px) => {
    return px * deviceWidth / uiWidth;
}



const checkPhone = (phone) => {
    if (!(/^1[3456789]\d{9}$/.test(phone))) {
        return false;
    } else {
        return true
    }
}

const checkPassword = (user) => {
    if (user === '') {
        return false
    } else {
        return true
    }
}

const MygoBack = (props) => {
    return BackHandler.addEventListener('hardwareBackPress', () => {
        props.navigation.goBack(null);
        if (props.nav.index === 0 && props.nav.routes[0].index === 0) {
            if (Date.now() - time > 2000) {
                ToastAndroid.show('是否退出应用', 100);
                time = Date.now();
                return true;
            } else {
                BackHandler.exitApp();
            }
            return true
        }

        return true
    });
}

export {
    pxPage,
    debounce,
    throttle,
    checkPhone,
    checkPassword,
    MygoBack
};
